package com.itheima.jdbc;



/*
 *  JDBC 快速入门
 * */

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo4_Statement {

    /*
    * 执行DML语句
    * */

    @Test
    public void testDML() throws Exception {

            // 注册驱动  新版可以去掉
//        Class.forName("com.mysql.jdbc.Driver");

            // 2.获取链接
            String url ="jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC";
            String username ="root";
            String password ="dzl123";
            Connection conn = DriverManager.getConnection(url,username,password);

            //3.定义sql
            String sql ="update account set money = 3000 where id = 5";

            //4.获取执行sql的对象statement
            Statement stmt = conn.createStatement();


            // 5.执行sql
            int count = stmt.executeUpdate(sql);//执行完DML语句后受影响的行数

            //6.处理结果
            //System.out.println(count);
        if (count>0){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }

            //7.释放资源
            stmt.close();
            conn.close();
    }
    /*
     * 执行DDL语句
     * */

    @Test
    public void testDDL() throws Exception {

        // 注册驱动  新版可以去掉
//        Class.forName("com.mysql.jdbc.Driver");

        // 2.获取链接
        String url ="jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC";
        String username ="root";
        String password ="dzl123";
        Connection conn = DriverManager.getConnection(url,username,password);

        //3.定义sql
        String sql ="drop  database db2";

        //4.获取执行sql的对象statement
        Statement stmt = conn.createStatement();

        // 5.执行sql
        int count = stmt.executeUpdate(sql);//执行完DDL语句,返回的可能是0

        //6.处理结果
        System.out.println(count);
        /*if (count>0){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }*/

        //7.释放资源
        stmt.close();
        conn.close();
    }
}
